/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.suarte.webapp.servlet;

import com.suarte.core.Invoice;
import com.suarte.core.Payment;
import com.suarte.core.Quotation;
import com.suarte.core.service.InvoiceManager;
import com.suarte.core.service.PaymentManager;
import com.suarte.core.service.QuotationManager;
import com.suarte.reports.InvoiceFormatToPdf;
import com.suarte.reports.QuotationToPdf;
import com.suarte.reports.ReceiptFormatToPdf;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * @date   Mar 14, 2011
 * @author Ggutierrez
 */
public class PrintServlet extends HttpServlet {

    private ApplicationContext appContext;
    private InvoiceManager invoiceManager;
    private QuotationManager quotationManager;
    private PaymentManager paymentManager;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (appContext == null) {
            appContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            invoiceManager = (InvoiceManager) appContext.getBean("invoiceManager");
            quotationManager = (QuotationManager) appContext.getBean("quotationManager");
            paymentManager = (PaymentManager) appContext.getBean("paymentManager");
        }

        Long id = 0L;
        String myType = request.getParameter("type");
        if (myType.equals("invoice")) {
            response.setContentType("application/pdf");

            id = Long.valueOf(request.getParameter("id"));
            Invoice invoice = invoiceManager.get(id);

            InvoiceFormatToPdf pdf = new InvoiceFormatToPdf();
            pdf.write(invoice, response.getOutputStream());

            response.getOutputStream().close();
        }

        if (myType.equals("quotation")) {
            response.setContentType("application/pdf");

            id = Long.valueOf(request.getParameter("id"));
            Quotation quotation = quotationManager.get(id);

            QuotationToPdf pdf = new QuotationToPdf();
            pdf.write(quotation, response.getOutputStream());

            response.getOutputStream().close();
        }

        if (myType.equals("receipt")) {
            response.setContentType("application/pdf");

            id = Long.valueOf(request.getParameter("id"));
            Payment payment = paymentManager.get(id);

            ReceiptFormatToPdf pdf = new ReceiptFormatToPdf();
            pdf.write(payment, response.getOutputStream());

            response.getOutputStream().close();
        }
    }

    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
